package weifei.live.simulation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import weifei.live.simulation.entity.Sort;
import weifei.web.Result;

@RestController
@RequestMapping("/sort")
public class SortController
{
    @Autowired
    private Sort dao_sort;


    @GetMapping("/getAllSortList")
    @PostMapping("/getAllSortList")
    public Result getAllSortList()
    {
        var list = new Sort().selectAll();
        return Result.success("获取成功", list);
    }


    @GetMapping("/getSortByID")
    @PostMapping("/getSortByID")
    public Result getSortByID(@RequestParam("id") Integer sortId)
    {
        if (sortId == null)
            return Result.exception("参数错误");

        QueryWrapper<Sort> qw_sort = new QueryWrapper<>();
        qw_sort.eq(Sort.Column.ID.getName(), sortId);
        var sort = this.dao_sort.selectOne(qw_sort);
        if (sort == null)
            return Result.failed("分类不存在");
        return Result.success("获取成功", sort);
    }


    @GetMapping("/getSortNameByID")
    @PostMapping("/getSortNameByID")
    public Result getSortNameByID(@RequestParam("id") Integer sortId)
    {
        if (sortId == null)
            return Result.exception("参数错误");

        QueryWrapper<Sort> qw_sort = new QueryWrapper<>();
        qw_sort.eq(Sort.Column.ID.getName(), sortId);
        var sort = this.dao_sort.selectOne(qw_sort);
        if (sort == null)
            return Result.failed("分类不存在");
        return Result.success("获取成功", sort.getName());
    }


    @GetMapping("/getSortByName")
    @PostMapping("/getSortByName")
    public Result getSortByName(@RequestParam("name") String name)
    {
        if (name == null)
            return Result.exception("参数错误");

        QueryWrapper<Sort> qw_sort = new QueryWrapper<>();
        qw_sort.eq(Sort.Column.Name.getName(), name);
        var sort = this.dao_sort.selectOne(qw_sort);
        if (sort == null)
            return Result.failed("分类不存在");
        return Result.success("获取成功", sort);
    }


    @GetMapping("/getSortIDByName")
    @PostMapping("/getSortIDByName")
    public Result getSortIDByName(@RequestParam("name") String name)
    {
        if (name == null)
            return Result.exception("参数错误");

        QueryWrapper<Sort> qw_sort = new QueryWrapper<>();
        qw_sort.eq(Sort.Column.Name.getName(), name);
        var sort = this.dao_sort.selectOne(qw_sort);
        if (sort == null)
            return Result.failed("分类不存在");
        return Result.success("获取成功", sort.getName());
    }


    @GetMapping("/searchSortByName")
    @PostMapping("/searchSortByName")
    public Result searchSortByName(@RequestParam("name") String searchName)
    {
        if (searchName == null)
            return Result.exception("参数错误");

        QueryWrapper<Sort> qw_sort = new QueryWrapper<>();
        qw_sort.like(Sort.Column.Name.getName(), searchName);
        var list = this.dao_sort.selectList(qw_sort);
        return Result.success("搜索成功", list);
    }


}

